﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace StreetlightGames.XnaEngine
{
    public class Camera : GameComponent, ICamera
    {
        /// <summary>Gets or sets the Position value</summary>
        public Vector3 Position
        {
            get { return _position; }
            set { _position = value; }
        }
        private Vector3 _position;

        /// <summary>Gets or sets the LookAt value</summary>
        public Vector3 LookAt
        {
            get { return _lookAt; }
            set { _lookAt = value; }
        }
        private Vector3 _lookAt;

        /// <summary>Gets or sets the Up value</summary>
        public Vector3 Up
        {
            get { return _up; }
            set { _up = value; }
        }
        private Vector3 _up;

        /// <summary>Gets the ProjectionMatrix value</summary>
        public Matrix ProjectionMatrix
        {
            get { return _projectionMatrix; }
        }
        private Matrix _projectionMatrix;
        
        /// <summary>Gets the ViewMatrix value</summary>
        public Matrix ViewMatrix
        {
            get { return _viewMatrix; }
        }
        private Matrix _viewMatrix;

        /// <summary>Gets or sets the NearPlaneDistance value</summary>
        public float NearPlaneDistance
        {
            get { return _nearPlaneDistance; }
            set { _nearPlaneDistance = value; }
        }
        private float _nearPlaneDistance;

        /// <summary>Gets or sets the FarPlaneDistance value</summary>
        public float FarPlaneDistance
        {
            get { return _farPlaneDistance; }
            set { _farPlaneDistance = value; }
        }
        private float _farPlaneDistance;

        /// <summary>Gets the BoundingFrustum value</summary>
        public BoundingFrustum BoundingFrustum
        {
            get { return _boundingFrustum; }
        }
        private BoundingFrustum _boundingFrustum;

        public Camera(Game game)
            : base(game)
        {
        }

        public Camera(Game game, Vector3 position, Vector3 lookAt, Vector3 up, float nearPlaneDistance, float farPlaneDistance)
            : base(game)
        {
            Position = position;
            LookAt = lookAt;
            Up = up;
            NearPlaneDistance = nearPlaneDistance;
            FarPlaneDistance = farPlaneDistance;
        }

        public override void Update(GameTime gameTime)
        {
            _viewMatrix = Matrix.CreateLookAt(
                Position,
                LookAt,
                Up);

            _projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45.0f),
                Game.GraphicsDevice.Viewport.AspectRatio,
                NearPlaneDistance,
                FarPlaneDistance);

            _boundingFrustum = new BoundingFrustum(ViewMatrix * ProjectionMatrix);
            base.Update(gameTime);
        }
    }
}
